package com.xinqi.modules.chat.chat.official.service.impl;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.xinqi.common.core.constant.ChatRemindConstants;
import com.xinqi.common.core.constant.MQConstants;
import com.xinqi.common.core.enums.RocketMqEnum;
import com.xinqi.common.core.enums.RocketMqRongyunEnum;
import com.xinqi.common.rocketmq.dto.ChatMessageMessageBody;
import com.xinqi.common.rocketmq.dto.MqMessageDTO;
import com.xinqi.common.rocketmq.event.ProducerEvent;
import com.xinqi.modules.chat.chat.official.dto.rsp.ChatAssistantContentMessageResultDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @version V1.0
 * @Title:融云课程助手MQ
 * @ClassName: com.xinqi.modules.chat.chat.official.service.impl.ChatAssistantMQService.java
 * @Description:
 * @Copyright 星齐科技（云南）有限公司 - Powered By 技术部
 * @author: yyzhen
 * @date: 2023/8/1 10:15
 */
@Log4j2
@Validated
@Service("chatAssistantMQService")
@RequiredArgsConstructor
public class ChatAssistantMQService {

    private final IdentifierGenerator identifierGenerator;

    /**
     * 通过助手好友ID发送助手消息
     */
    @Async("threadPoolTaskExecutor")
    public void sendAssistantList(ChatAssistantContentMessageResultDTO messageResult, String pushContent, String objectName, Long senderId, List<Long> targetIdList) {
        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_ASSISTANT_MESSAGE.getCode());
        body.setSenderId(senderId.toString());

        // 设置批量发布的用户信息
        List<String> targetIds = targetIdList.stream().map(Object::toString).collect(Collectors.toList());
        body.setTargetIds(targetIds.toArray(new String[targetIds.size()]));

        body.setJson(com.alibaba.fastjson.JSONObject.toJSONString(messageResult));
        body.setPushContent(pushContent);
        body.setObjectName(objectName);
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), com.alibaba.fastjson.JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.REMIND_MESSAGE_KEY + identifierGenerator.nextId(""));
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 修改课程助手消息
     *
     * @param messageResult
     * @param objectName
     * @return
     */
    @Async("threadPoolTaskExecutor")
    public void updateAssistant(ChatAssistantContentMessageResultDTO messageResult, String objectName) {
        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_ASSISTANT_UPDATE_MESSAGE.getCode());
        body.setSenderId(ChatRemindConstants.ASSISTANT);
        body.setTargetIds(new String[]{messageResult.getRecipient().toString()});
        body.setJson(com.alibaba.fastjson.JSONObject.toJSONString(messageResult));
        body.setObjectName(objectName);
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), com.alibaba.fastjson.JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.REMIND_MESSAGE_KEY + messageResult.getRecipient());
        ProducerEvent.producerFifoMessage(message);
    }


}
